from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import DatetimeString, StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.orders_fields_def import order_fields,order_list_fields
from services.online_mall.order_service import OrserService

class OrdersApi(Resource):
    @login_required
    @marshal_with(order_list_fields)
    @check_perms(perms=["mall:order:list"])
    def get(self):
        parser= reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("account_id",type=str,required=False,location='args')
        parser.add_argument("order_no",type=str,required=False,location='args')
        parser.add_argument("status",type=str,required=False,location='args')
        parser.add_argument("pay_type",type=int,required=False,location='args')
        parser.add_argument("dvy_flow_id",type=str,required=False,location='args')
        parser.add_argument("is_payed",type=StrBoolNone,required=False,location='args')
        parser.add_argument("del_status",type=str,required=False,location='args')
        parser.add_argument("refund_status",type=str,required=False,location='args')
        parser.add_argument("order_type",type=int,required=False,location='args')
        parser.add_argument("close_type",type=str,required=False,location='args')
        parser.add_argument("page",type=int,required=False,location='args')
        parser.add_argument("page_size",type=int,required=False,location='args')
        args= parser.parse_args()
        return OrserService.get_order_list(current_user,args)
    
    @login_required
    @marshal_with(order_fields)
    @check_perms(perms=["mall:order:delivery"])
    def put(self):
        parser= reqparse.RequestParser()
        parser.add_argument("order_no",type=str,required=True,location='json')
        parser.add_argument("dvy_id",type=str,required=True,location='json')
        parser.add_argument("dvy_flow_id",type=str,required=True,location='json')
        args= parser.parse_args()
        order_no = args.get("order_no")
        dvy_id = args.get("dvy_id")
        dvy_flow_id = args.get("dvy_flow_id")
        return OrserService.delivery_order(current_user,order_no,dvy_id,dvy_flow_id)
    
class SingleOrdersApi(Resource):
    @login_required
    @marshal_with(order_fields)
    @check_perms(perms=["mall:order:read"])
    def get(self,order_no:str):
        return OrserService.get_order_by_order_no(current_user,order_no)
    
class DeliveryOrdersApi(Resource):
    @login_required
    @marshal_with(order_list_fields)
    @check_perms(perms=["mall:order:dvy_list"])
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('shop_id',type=str,required=False,location='json')
        parser.add_argument('start', type=DatetimeString("%Y-%m-%d %H:%M"), required=False, help='start time',location='json')
        parser.add_argument('end', type=DatetimeString("%Y-%m-%d %H:%M"), required=False, help='end time',location='json')
        parser.add_argument('page', type=int, required=False, default=-1, help='page',location='json')
        parser.add_argument('page_size', type=int, required=False, default=10, help='page size',location='json')
        args = parser.parse_args()
        shop_id = args.get('shop_id')
        start = args.get('start')
        end = args.get('end')
        page = args['page']
        page_size = args['page_size']
        return OrserService.deliveried_orders(current_user,shop_id,start,end,page,page_size)
    
class SoldOrdersApi(Resource):
    @login_required
    @marshal_with(order_list_fields)
    @check_perms(perms=["mall:order:sold_list"])
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('shop_id',type=str,required=False,location='json')
        parser.add_argument('start', type=DatetimeString("%Y-%m-%d %H:%M"), required=False, help='start time',location='json')
        parser.add_argument('end', type=DatetimeString("%Y-%m-%d %H:%M"), required=False, help='end time',location='json')
        parser.add_argument('page', type=int, required=False, default=-1, help='page',location='json')
        parser.add_argument('page_size', type=int, required=False, default=10, help='page size',location='json')
        args = parser.parse_args()
        shop_id = args.get('shop_id')
        start = args.get('start')
        end = args.get('end')
        page = args['page']
        page_size = args['page_size']
        return OrserService.sold_orders(current_user,shop_id,start,end,page,page_size)
    
api.add_resource(SoldOrdersApi,'/orders/sold')
api.add_resource(DeliveryOrdersApi,'/orders/wait_delivery')
api.add_resource(OrdersApi, '/orders')
api.add_resource(SingleOrdersApi, '/orders/<string:order_no>')